package com.health.system.action.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import com.health.system.javacommon.jquery.JqueryPageHelper;
import com.health.system.javacommon.jquery.ListRange;
import com.health.system.javacommon.util.StringTool;
import com.health.system.model.system.Role;

import com.health.system.model.system.Permission;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;
import static com.health.system.javacommon.util.Struts2JsonHelper.*;

import com.health.system.javacommon.base.*;

import com.health.system.model.system.*;
import com.health.system.dao.system.*;
import com.health.system.service.system.impl.*;
import com.health.system.service.system.*;

/**
 * <p>Title: 权限信息 Action</p> 
 * Description: 页面通过Ajax调用Action层,返回JSONO数据
 * Copyright: Copyright (c) 2011-03 
 * Company: 
 * 
 * @author 
 * @version 1.0 
*/

public class PermissionAction extends BaseStruts2Action {
	
	private PermissionManager permissionManager;
	
	/** 通过spring自动注入 */
	public void setPermissionManager(PermissionManager manager) {
		this.permissionManager = manager;
	}	
	
	/**
	 * jqGrid使用列表
	 * @throws IOException
	 */
	public void list() throws IOException{
		PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
		Page page = permissionManager.findByPageRequest(pr);
		
		List<Permission> Permissionlist = (List) page.getResult();
		ListRange<Permission> resultList = new ListRange<Permission>();
		resultList.setDataRows(Permissionlist);
        resultList.setRecords(page.getTotalCount());
        resultList.setRows(page.getPageSize());
		if(page.getTotalCount()%page.getPageSize()>0){
			resultList.setTotal(page.getTotalCount() / page.getPageSize()+1);
		}else{
			resultList.setTotal(page.getTotalCount() / page.getPageSize());
		}
		resultList.setPage(pr.getPageNumber());
		outJson(resultList);
	}

	/**
	 * Permission添加
	 * @throws IOException
	 */
	public void save() throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			HttpServletRequest request = getRequest();
			String permissionJson = request.getParameter("permissionJson");
			if (null != permissionJson&&!"".equals(permissionJson)) {
				JSONObject jsonobject =JSONObject.fromObject( permissionJson );
				Permission permissionobj = (Permission)JSONObject.toBean(jsonobject, Permission.class); 
				permissionManager.save(permissionobj);
				result.put("success", true);
				result.put("msg", "添加成功!");
			}else{
				result.put("success", false);
				result.put("msg", "添加失败 数据错误!");
			}
		}catch (Exception e){
			result.put("success", false);
			result.put("msg", "添加失败!");
			e.printStackTrace();
		}
		outJson(result);
	}
	
	/**
	 * Permission修改
	 * @throws IOException
	 */
	public void update() throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
			try{
				HttpServletRequest request = getRequest();
				String permissionJson = request.getParameter("permissionJson");
				if (null != permissionJson&&!"".equals(permissionJson)) {
					JSONObject jsonobject =JSONObject.fromObject( permissionJson );
					Permission permissionobj = (Permission)JSONObject.toBean(jsonobject, Permission.class); 
					permissionobj.setId(jsonobject.get("id").toString());
					permissionManager.update(permissionobj);
					result.put("success", true);
					result.put("msg", "修改成功!");
				}else{
					result.put("success", false);
					result.put("msg", "修改失败 数据错误!");
				}
			}catch (Exception e){
				result.put("success", false);
				result.put("msg", "修改失败!");
				e.printStackTrace();
			}
		outJson(result);
	}
	
	/**
	 * Permission删除
	 * @throws IOException
	 */
	public void delete() throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			HttpServletRequest request = getRequest();
			String ids = request.getParameter("ids");
			if (null != ids&&!"".equals(ids)){
				Permission permission =new Permission();
				permission.setIdList(StringTool.stringToArray(ids));
				permissionManager.removeByIdArry(permission);
				result.put("success", true);
				result.put("msg", "删除成功");
			}else{
				result.put("success", false);
				result.put("msg", "删除失败 数据错误!");
			}
		}catch (Exception e){
			result.put("success", false);
			result.put("msg", "删除失败!");
			e.printStackTrace();
		}
		outJson(result);
	}
	
	public void checkPermissionName() throws IOException{
		try {
			HttpServletRequest request = getRequest();
			String name = request.getParameter("permissionName");
			if(name!=null&&!"".equals(name)){
				List list = (List)permissionManager.getPermissionByName(name);
				if(list != null &list.size() > 0){
					getResponse().getWriter().print(false);
				}else {
					getResponse().getWriter().print(true);
				}
			}
		} catch (Exception e) {
			getResponse().getWriter().print(false);
			e.printStackTrace();
		}
	}
	
}
